﻿using Simple.ISysRepository;
using Simple.ISysService;
using Simple.SysRepository;
using Simple.Tools;
using Simple.Tools.Extensions;
using Simple.Tools.SnowFlake;
using Simple.Tools.Verify;
using SimpleSaas.Common;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;
using SimpleSaas.Entity.Params;
using SimpleSaas.Entity.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysService
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	/// <summary>
	/// 
	/// </summary>
	public class SysDatabaseService : ISysDatabaseService
	{
		/// <summary>
		/// 
		/// </summary>
		[Autowired]
		protected ISysDatabaseRepository sysDatabaseRepository { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Autowired]
		protected ISimpleMapper simpleMapper { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Autowired]
		protected ISysTenantRepository sysTenantRepository { get; set; }

		/// <summary>
		/// 添加数据链接配置
		/// </summary>
		/// <param name="sysDatabaseDto"></param>
		/// <returns></returns>
		/// <exception cref="SystemException"></exception>
		public async Task AddAsync(SysDatabaseDto sysDatabaseDto)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			var verify = sysDatabaseDto.Verify();

			if (!verify.Item2)
			{
				throw new SystemException(verify.Item1);
			}

			if (await sysDatabaseRepository.AnyAsync(database => !database.IsDeleted && database.DisplayName.Equals(sysDatabaseDto.DisplayName)))
			{
				throw new SystemException("名称重复!");
			}

			SysDatabaseEntity sysDatabase = new SysDatabaseEntity();
			simpleMapper.Map(sysDatabaseDto, sysDatabase);
			await sysDatabaseRepository.CreateAsync(sysDatabase);
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <returns></returns>
		public async Task UpdateAsync(SysDatabaseDto sysDatabaseDto)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			var verify = sysDatabaseDto.Verify();

			if (!verify.Item2)
			{
				throw new SystemException(verify.Item1);
			}


			SysDatabaseEntity sysDatabase = await sysDatabaseRepository.FirstOrDefaultAsync(database => database.Id.Equals(sysDatabaseDto.Id.ToInt64()) && !database.IsDeleted);

			if (SimpleCheck.IsNull(sysDatabase))
			{
				throw new SystemException("更新失败!");
			}

			if (await sysTenantRepository.AnyAsync(t => t.DatabaseId == sysDatabase.Id))
			{
				throw new SystemException("更新失败,已经有企业选择使用该数据库!");
			}
			if (await sysDatabaseRepository.AnyAsync(database =>
			!database.Id.Equals(sysDatabaseDto.Id.ToInt64()) && !database.IsDeleted
			&& database.DisplayName.Equals(sysDatabaseDto.DisplayName)))
			{
				throw new SystemException("名称重复!");
			}
			simpleMapper.Map(sysDatabaseDto, sysDatabase);

			await sysDatabaseRepository.UpdateAsync(sysDatabase);
		}

		/// <summary>
		///  软删除
		/// </summary>
		/// <param name="sysDatabaseDto"></param>
		/// <returns></returns>
		public async Task SoftDeleteAsync(SysDatabaseDto sysDatabaseDto)
		{
			SysDatabaseEntity sysDatabase = await sysDatabaseRepository.FirstOrDefaultAsync(database => database.Id.Equals(sysDatabaseDto.Id.ToInt64()) && !database.IsDeleted);

			if (SimpleCheck.IsNull(sysDatabase))
			{
				throw new SystemException("删除失败!");
			}

			if (await sysTenantRepository.AnyAsync(t => t.DatabaseId == sysDatabase.Id))
			{
				throw new SystemException("删除失败,已经有企业选择使用该数据库!");
			}

			await sysDatabaseRepository.SoftDeleteAsync(sysDatabase);
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name="sysDatabaseDto"></param>
		/// <returns></returns>
		public async Task<PageList<SysDatabaseView>> GetPageAsync(SysDatabaseParameter Params)
		{
			var query= sysDatabaseRepository.Query(q =>
			{
				q.OrderByDescending(dataBase=>dataBase.CreationTime);
			});

			return await PageList<SysDatabaseView>.CreateAsync(query, Params);
		}

		/// <summary>
		///  通过id查询详情
		/// </summary>
		/// <param name="sysDatabaseDto"></param>
		/// <returns></returns>
		public async Task<SysDatabaseView> GetByIdAsync(SysDatabaseParameter Params)
		{
			SysDatabaseView sysDatabase = await sysDatabaseRepository.Query(q =>
			{
				q.Where(dataBase=>dataBase.Id== Params.Id.ToInt64() &&!dataBase.IsDeleted);

			}).FirstAsync();

			return sysDatabase;
		}

	}
}
